package com.example.redistest.ThreadDemo;

/**
 * 银行
 */
public class Bank {
    //银行名
    private String name;
    private Double blanance;//余额

    public Bank(String name,Double blanance){
        this.blanance = blanance;
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getBlanance() {
        return blanance;
    }

    public void setBlanance(Double blanance) {
        this.blanance = blanance;
    }

    //取款
    public void takeMoney(Double money){
        /*
        把需要同步的代码放到,synchronized代码块中
        线程执行到这里就会去找锁,如果找到对象锁就
        会执行代码,没有找到对象锁就会等待
        t1和t2两个线程
        t1执行到synchronized先获得对象锁,t2会等待t1释放
        对象锁
         */
        synchronized (this){
            Double after = blanance-money;
            try {
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
            this.setBlanance(after);//更新余额
        }
    }

}
